/**
 *
 *  @auth xlx_good@qq.com
 *  @date 17/6/21.
 *
 */

requirejs.config({
    baseUrl: '../../',
    paths:  {
        avalon: 'javascript/avalon.min',
        jquery: 'js/plugins/jquery/jquery-2.1.1.min',
        pagination: 'lib/pagination/jquery.pagination',
        config: 'javascript/config',
        api: 'javascript/api',
        layer: 'js/plugins/layer1/layer',
		select2: 'js/plugins/select2/select2',
        arg: 'javascript/arg'
    },
    shim: {
        pagination: {
            deps: ['jquery']
        },
        api: {
            deps: ['jquery']
        },
        layer: {
            deps: ['jquery']
        }
    }
});


(function(win, dom, requirejs, initVm){
    var vm;
    requirejs(['avalon', 'jquery', 'pagination', 'config', 'api', 'layer','select2','arg'], function(){
        //初始化vm
        vm = initVm(dom);
    });
	win.gbWareUpdate = function(callback){};
	win.gbWareDrafts = function(callback){};
})(window, document, requirejs, function(dom){
    var vm = avalon.define({
        $id: 'container',
        form: {
			categoryId: '',
			saleSiteId: '',
			id: '',
			name: '',
			barcode: '',
			brandName: '',
			pageNum: 1,
			pageSize: 10
		},
		keyword: '',
		categoryList: [],
		wareCategoryList: function() {
			Api.wareCategoryList(function(resp) {
				if(resp.code == 0) {
					vm.categoryList = resp.data;
				} else {
					layer.msg(resp.msg);
				}
			});
		},
		siteList: [],
		supplierSiteList: function() {
			Api.supplierSiteList(function(resp) {
				if(resp.code == 0) {
					vm.siteList = resp.data;
				} else {
					layer.msg(resp.msg);
				}
			});
		},
		keywordList: {
			"id": "商品ID",
			"name": "商品名称",
			"barcode": "商品条码",
			"brandName": "商品品牌"
		},
		list: [],
        search: function(){
			for(var key in vm.keywordList) {
				vm.form[key] = null;
			}
			vm.form[$('#selKeyword').val()] = vm.keyword;
			Api.wareForSalePage(vm.form, function(resp) {
				if(resp.code == 0) {
					if(vm.form.pageNum == 1) {
						setPagination(resp.data.total, resp.data.pageNum - 1, vm);
					}
					vm.list = resp.data.list;
				} else {
					layer.msg(resp.msg);
				}
			});
        },
		wareOnSale: function(wareId) {
			layer.confirm('你确定要上架？', function(index) {
				var form = {
					id: wareId
				}
				Api.wareOnSale(form, function(resp) {
					if(resp.code == 0) {
						layer.msg(resp.msg);
						vm.search();
					} else {
						layer.msg(resp.msg);
					}
				});
				layer.close(index);
			})
		},
		showManage: function(wareId) {
			var params = {
				id: wareId
			}
			var url = Arg.url("./manage.html", params);
            layer.open({
                title: '商品管理',
                type: 2,
                area: ['660px','320px'],
                shadeClose: false,
                content: [url,'no'],
                btn: ['确定', '取消','变更为草稿'],
                yes: function(index,layero) {
					gbWareUpdate(function(resp) {
						if(resp.code == 0) {
							layer.close(index);
						}
						layer.msg(resp.msg);
						vm.search();
					});
                },
				btn2: function(index,layero){
					
				},
				btn3: function(index,layero){
					gbWareDrafts(function(resp) {
						if(resp.code == 0) {
							layer.close(index);
						}
						layer.msg(resp.msg);
						vm.search();
					});
				}
            })
		}
    })
	avalon.filters.filterStatus = function(status) {
		return wareStatusMap[status];
	}
	avalon.filters.filterOption = function(ware) {
		//1-草稿,2-未上架,3-已上架,4-已删除,5-待审核,6-审核不通过
		if(ware.status == 2) {
			return '<a href="javascript:;" :click="@wareOnSale(ware.id)">上架</a>&nbsp;&nbsp;<a href="javascript:;" :click="@showManage(ware.id)">管理</a>';
		}
		return '';
	}

    $("#saleArea").select2({});
    avalon.scan(dom.body);
	vm.wareCategoryList();
	vm.supplierSiteList();
	vm.search();
    return vm;
});

/**
 * 设置翻页
 * @param <number> 总条数
 * @param <number> 当前页码
 * */
function setPagination(total, current, vm){
    $('.pagination').pagination(total,{
        items_per_page: 10, //  每页显示条数
        current_page: current,
        num_display_entries: 20, // 显示的页面个数
        next_text: '下一页',
        prev_text: '上一页',
        num_edge_entries: 2, //省略号后面显示的页码个数
        callback: function(index){
			if(vm.form.pageNum != (index + 1)){
				vm.form.pageNum = index + 1;
			    vm.search();
			}
        }
    });
}
